{% extends 'myadmin/index.html' %}

{% block title %}
<title>后台-订单列表</title>
{% endblock %}

{% block content %}
<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title  am-cf" style="font-size: 26px;color: #3599d6;">订单列表</div></div>
                <div class="widget-body  am-fr">
                    <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                        <div class="am-form-group">
                            <div class="am-btn-toolbar">
                                <div class="am-btn-group am-btn-group-xs">
                                </div>
                            </div>
                        </div>
                    </div>
                    <form>
                        <div class="am-u-sm-12 am-u-md-6 am-u-lg-3">
                            <div class="am-form-group tpl-table-list-select">
                                <select name="types" data-am-selected="{btnSize: 'sm'}" style="display: none;">                             
                                    <option value="receiver" {% if request.GET.types == 'receiver' %} selected {% endif %}>收件人</option>
                                    <option value="orderman" {% if request.GET.types == 'uid.username' %} selected {% endif %}>购买者</option>
                                </select>
                            </div>
                        </div>
                        <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
                            <div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
                                <input value="{{ request.GET.keywords }}" name="keywords" type="text" class="am-form-field ">
                                <span class="am-input-group-btn">
                                    <button class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search"></button>
                                </span>
                            </div>
                        </div>
                    </form>
                    <div class="am-u-sm-12">
                        <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black ">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>状态</th>
                                    <th>收件人</th>
                                    <th>收件电话</th>
                                    <th>收件地址</th>
                                    <th>总价</th>
                                    <th>下单时间</th>
                                    <th>付款时间</th>
                                    <th>购买人</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for u in data %}
                                <tr class="gradeX">
                                    <td class="am-text-middle orderid" id="{{ u.id }}">{{ u.id }}</td>
                                    <td class="am-text-middle editstatus">
                                        {% if u.status == 0 %}   
                                            <span class="name">未支付</span>
                                        {% elif u.status == 1 %}
                                            <span class="name">未发货</span>
                                        {% elif u.status == 2 %}
                                            <span class="name">已发货</span>
                                        {% elif u.status == 3 %}               
                                            <span class="name">已收货</span>
                                        {% endif %}
                                    </td>
                                    <td class="am-text-middle">{{ u.receiver }}</td>
                                    <td class="am-text-middle">{{ u.receivephone }}</td>
                                    <td class="am-text-middle">
                                        {{ u.receiveaddress }}
                                    </td>
                                    <td class="am-text-middle">{{ u.totalprice }}</td>
                                    <td class="am-text-middle">{{ u.addtime }}</td>
                                    {% if u.buytime == None %}
                                    <td class="am-text-middle"></td>
                                    {% else %}
                                    <td class="am-text-middle">{{ u.buytime }}</td>
                                    {% endif %}
                                    <td class="am-text-middle">{{ u.uid.username }}</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    <div class="am-u-lg-12 am-cf">
                        <div class="am-fr">
                            <ul class="am-pagination tpl-pagination">
                                {% load pagetag %}
                                {% showpages data.paginator.num_pages request %}
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $('.orderid').click(function(){
        var orderid = $(this).attr('id')
        console.log(orderid)
        location.href="{% url 'myadmin_order_info' %}?orderid="+orderid
    })

    // 给td绑定双击事件,进行修改分类名
    $('tbody .editstatus').dblclick(function(){
        // 获取当前的状态
        var name = $(this).find('.name').text()
        var oid = $(this).prev().text()
        console.log(oid)
        // 动态创建input,放到name的位置
        var inp = $('<input value="'+name+'" style="color:black;">')
        $(this).find('.name').html(inp)
        inp.select()
        var t = $(this)

        // 给input绑定丧失焦点事件
        inp.blur(function(){
            // 获取当前input中修改的值
            var newstatus = $(this).val()
            // 判断新的值和原来的值是否一致
            if( newstatus == name ){
                // 恢复原样
                t.find('.name').html(name)
            }else{
                // 发送ajax请求去修改
                $.get('{% url "myadmin_order_status" %}',{'oid':oid,'name':newstatus},function(data){
                    if(data['error'] == 0){
                        // 恢复原样
                        t.find('.name').html(newstatus)
                        alert(data['msg'])
                    }
                    if(data['error'] == 1){
                        // 恢复原样
                        t.find('.name').html(name)
                        alert(data['msg'])
                    }
                    
                },'json')
            }
        })
    })
</script>
{% endblock %}